package com.example.software.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

// import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.example.software.entity.Result;
import com.example.software.utils.PageUtils;
import com.example.software.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.example.software.entity.MedicineEntity;
import com.example.software.service.MedicineService;




/**
 * 
 *
 * @author wxy
 * @email wxy9496099@163.com
 * @date 2024-10-31 21:55:40
 */
@RestController
@Slf4j
@Api(tags = "药品相关接口")
@RequestMapping("software/medicine")
public class MedicineController {
    @Autowired
    private MedicineService medicineService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "药品信息列表查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = true),
            @ApiImplicitParam(name = "size", value = "每页数量",dataType = "int"),
            @ApiImplicitParam(name = "medicineTypeId", value = "药品类型id，如果查全部可以不传或者传0"),
            @ApiImplicitParam(name = "name", value = "模糊查询，如果查全部可以不传或者传0"),
            @ApiImplicitParam(name = "state", value = "状态，0为下架，1为上架，不传为全部")
    })
   // @RequiresPermissions("software:medicine:list")
    public Result<PageUtils> list(@RequestParam String page,
                                  @RequestParam String size,
                                  @RequestParam(required = false)  Long medicineTypeId,
                                  @RequestParam(required = false)   String name,
                                  @RequestParam(required = false) Integer state){

        Map<String, Object> params = new HashMap<>();
        params.put("page",page);
        params.put("limit",size);

        if (medicineTypeId != null && medicineTypeId != 0) {
            params.put("medicineTypeId", medicineTypeId);
        }

        if (name != null && !name.equals("0")) {
            params.put("name", name);
        }
        if (state != null) {
            params.put("state", state);
        }

        PageUtils page2 = medicineService.queryPage(params);

        return Result.success (page2);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "根据id药品信息查询")
    // @RequiresPermissions("software:medicine:info")
    public Result<MedicineEntity> info(@PathVariable("id") Long id){
		MedicineEntity medicine = medicineService.getById(id);

        return Result.success(medicine);
    }

    /**
     * 保存
     */
    @PostMapping
    @ApiOperation(value = "新增药品")
    // @RequiresPermissions("software:medicine:save")
    public R save(@RequestBody MedicineEntity medicine){
		medicineService.save(medicine);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation(value = "修改药品信息")
    // @RequiresPermissions("software:medicine:update")
    public R update(@RequestBody MedicineEntity medicine){
		medicineService.updateById(medicine);

        return R.ok();
    }

    //下架商品（修改接口）

    /**
     * 删除
     */
    @DeleteMapping
    @ApiOperation(value = "批量删除药品信息")
    // @RequiresPermissions("software:medicine:delete")
    public R delete(@RequestBody Long[] ids){
		medicineService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
